package com.fingard.dsp.bank.directbank.nnbill01;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.SendBankRespose;
import com.fingard.dsp.bank.directbank.bankInterface.IQryUserInfo;
import com.fingard.dsp.bank.reqrespobj.userinfo.ReqQryUserInfo;
import com.fingard.dsp.bank.reqrespobj.userinfo.RetAtsQryUserInfo;
import com.fingard.dsp.bank.reqrespobj.userinfo.detail.RetAtsQryUserInfoDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;

/**
 * 快钱用户信息查询
 * 
 * @author Cruise
 * @date 2018年10月31日
 */
public class NNBill01QryUserInfo extends NNBill01Base implements IQryUserInfo {
	@Override
	public RetAtsQryUserInfo qrySmsRecUserInfo(ReqQryUserInfo tmpQryReq) throws Exception {
		if (StringHelper.isNullOrEmpty(tmpQryReq.cardType)) {
			RetAtsQryUserInfo retAtsQryUserInfo = qrySmsRecUserInfoByUserID(tmpQryReq);
			return retAtsQryUserInfo;
		}
		
		RetAtsQryUserInfo retAts = new RetAtsQryUserInfo(tmpQryReq.reqHeader);
		retAts.writeStart();
		
		String tmpRespCode = "";
		String tmpRespInfo = "";

		try {
			ActSetItem actItem = getBankConfig().getActSetItem(tmpQryReq.recAct, "smsRec");
			if (actItem != null) {
				StringBuilder sbXml = new StringBuilder();
				
				String tmpCardType = "";
				if (StringHelper.hasAnyChar(tmpQryReq.cardType)) {
					if ("3".equals(tmpQryReq.cardType)) {
						tmpCardType = "0001";// 信用卡
					} else {
						tmpCardType = "0002";// 借记卡
					}
				}
				
				String tmpBankID = "";
				if (StringHelper.hasAnyChar(tmpQryReq.payBankCode)) {
					tmpBankID = getBankConfig().getMapValueByKey("bankMapping", tmpQryReq.payBankCode);
				}
				
				sbXml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
				sbXml.append("<MasMessage xmlns=\"http://www.99bill.com/mas_cnp_merchant_interface\">");
				sbXml.append("<version>1.0</version>");
				sbXml.append("<PciQueryContent>");
				sbXml.append("<merchantId>" + actItem.merchantId + "</merchantId>");
				sbXml.append("<customerId>" + tmpQryReq.userId + "</customerId>");
				sbXml.append("<cardType>" + tmpCardType + "</cardType>"); 
				sbXml.append("<bankId>" + tmpBankID + "</bankId>"); 
				sbXml.append("</PciQueryContent>");
				sbXml.append("</MasMessage>");

				String queryUrl = bankFront.getUrl("smsRecQueryUserInfo");
				this.WriteBankLogLn("查询用户信息服务地址：" + queryUrl);

				String reqXml = sbXml.toString();
				this.WriteBankLogStep2(reqXml);

				SendBankRespose smsResp = SmsPost.sendPost(queryUrl, actItem, reqXml, getCharset());

				WriteBankLogStep3(smsResp);
				
				if (smsResp.connState.length() == 0) {
					if (smsResp.retMessage.indexOf("<") >= 0) {
						XmlTextReader xmlReader = new XmlTextReader(smsResp.retMessage);
						if (StringHelper.hasAnyChar(xmlReader.getFirstText("/MasMessage/ErrorMsgContent/errorMessage"))) {
							tmpRespCode = "-1";
							tmpRespInfo = xmlReader.getFirstText("/MasMessage/ErrorMsgContent/errorMessage");
						} else {
							String responseCode = xmlReader.getFirstText("/MasMessage/PciQueryContent/responseCode");
							if ("00".equals(responseCode)) {
								tmpRespCode = "0";
								
								String userId = xmlReader.getFirstText("/MasMessage/PciQueryContent/customerId");
								
								String cardType = xmlReader.getFirstText("/MasMessage/PciQueryContent/cardType");
								String bankCardType = "";
								if (StringHelper.hasAnyChar(cardType)) {
									if ("0001".equals(cardType)) {
										bankCardType = "3";
									} else {
										bankCardType = "2";
									}
								}
								
								while(xmlReader.read("/MasMessage/PciQueryContent/pciInfos/pciInfo")) {
									RetAtsQryUserInfoDetail tmpRetDetail = new RetAtsQryUserInfoDetail();
									
									tmpRetDetail.cardType = bankCardType;
									String bankId = xmlReader.readedXElement.getSingleText("bankId");
									if (StringHelper.hasAnyChar(bankId)) {
										tmpRetDetail.payBankCode = getBankConfig().getMapValueByKey("bankMapping_qrySmsRecUserInfo", bankId);
									}
		                            tmpRetDetail.shortCardNo = xmlReader.readedXElement.getSingleText("storablePan");
		                            tmpRetDetail.shortPhoneNo = xmlReader.readedXElement.getSingleText("shortPhoneNo");
		                            tmpRetDetail.cellPhone = xmlReader.readedXElement.getSingleText("phoneNO");
		                            tmpRetDetail.userId = userId;
		                            
		                            retAts.writeRetDetail(tmpRetDetail);
		                        }
							} else {
								tmpRespCode = "-1";
								tmpRespInfo = xmlReader.getFirstText("/MasMessage/PciQueryContent/responseTextMessage");
							}
						}
					} else {
						tmpRespCode = "-1";
						tmpRespInfo = smsResp.retMessage;
					}
				} else {
					tmpRespCode = "-1";
					tmpRespInfo = smsResp.retMessage;
				}
			} else {
				tmpRespCode = "-1";
				tmpRespInfo = "未找到账号:[" + tmpQryReq.recAct + "]商户配置信息!";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRespCode = "-1";
			tmpRespInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		if (retAts.getWrittenRdCount() > 0) {
			tmpRespCode = "0";
		} else if ("-1".equals(tmpRespCode)) {
			retAts.writeRespCode(tmpRespCode);
			retAts.writeRespInfo(tmpRespInfo);
		} else if (retAts.getWrittenRdCount() == 0) {
			retAts.writeRespCode("0");
			retAts.writeRespInfo("客户信息为空");
		} 
		
		retAts.writeEnd();
		return retAts;
	}
	
	public RetAtsQryUserInfo qrySmsRecUserInfoByUserID(ReqQryUserInfo tmpQryReq) throws Exception {
		RetAtsQryUserInfo retAts = new RetAtsQryUserInfo(tmpQryReq.reqHeader);
		retAts.writeStart();
		
		String tmpRespCode = "";
		String tmpRespInfo = "";

		try {
			ActSetItem actItem = getBankConfig().getActSetItem(tmpQryReq.recAct, "smsRec");
			if (actItem != null) {
				String[] cardTypeArray = new String[] {"0001","0002"};
				for(String cardType : cardTypeArray) {
					StringBuilder sbXml = new StringBuilder();
					
					String tmpBankID = "";
					if (StringHelper.hasAnyChar(tmpQryReq.payBankCode)) {
						tmpBankID = getBankConfig().getMapValueByKey("bankMapping", tmpQryReq.payBankCode);
					}
					
					sbXml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
					sbXml.append("<MasMessage xmlns=\"http://www.99bill.com/mas_cnp_merchant_interface\">");
					sbXml.append("<version>1.0</version>");
					sbXml.append("<PciQueryContent>");
					sbXml.append("<merchantId>" + actItem.merchantId + "</merchantId>");
					sbXml.append("<customerId>" + tmpQryReq.userId + "</customerId>");
					sbXml.append("<cardType>" + cardType + "</cardType>"); 
					if (StringHelper.hasAnyChar(tmpBankID)) {
						sbXml.append("<bankId>" + tmpBankID + "</bankId>"); 
					}
					sbXml.append("</PciQueryContent>");
					sbXml.append("</MasMessage>");

					String queryUrl = bankFront.getUrl("smsRecQueryUserInfo");
					this.WriteBankLogLn("查询用户信息服务地址：" + queryUrl);

					String reqXml = sbXml.toString();
					this.WriteBankLogStep2(reqXml);

					SendBankRespose smsResp = SmsPost.sendPost(queryUrl, actItem, reqXml, getCharset());

					WriteBankLogStep3(smsResp);
					
					if (smsResp.connState.length() == 0) {
						if (smsResp.retMessage.indexOf("<") >= 0) {
							XmlTextReader xmlReader = new XmlTextReader(smsResp.retMessage);
							if (StringHelper.hasAnyChar(xmlReader.getFirstText("/MasMessage/ErrorMsgContent/errorMessage"))) {
								tmpRespCode = "-1";
								tmpRespInfo = xmlReader.getFirstText("/MasMessage/ErrorMsgContent/errorMessage");
							} else {
								String responseCode = xmlReader.getFirstText("/MasMessage/PciQueryContent/responseCode");
								if ("00".equals(responseCode)) {
									tmpRespCode = "0";
									
									String userId = xmlReader.getFirstText("/MasMessage/PciQueryContent/customerId");
									
									String bankCardType = "";
									if (StringHelper.hasAnyChar(cardType)) {
										if ("0001".equals(cardType)) {
											bankCardType = "3";
										} else {
											bankCardType = "2";
										}
									}
									
									while(xmlReader.read("/MasMessage/PciQueryContent/pciInfos/pciInfo")) {
										RetAtsQryUserInfoDetail tmpRetDetail = new RetAtsQryUserInfoDetail();
										
										tmpRetDetail.cardType = bankCardType;
										
										String bankId = xmlReader.readedXElement.getSingleText("bankId");
										if (StringHelper.hasAnyChar(bankId)) {
											tmpRetDetail.payBankCode = getBankConfig().getMapValueByKey("bankMapping_qrySmsRecUserInfo", bankId);
										}
			                            tmpRetDetail.shortCardNo = xmlReader.readedXElement.getSingleText("storablePan");
			                            tmpRetDetail.shortPhoneNo = xmlReader.readedXElement.getSingleText("shortPhoneNo");
			                            tmpRetDetail.cellPhone = xmlReader.readedXElement.getSingleText("phoneNO");
			                            tmpRetDetail.userId = userId;
			                            
			                            retAts.writeRetDetail(tmpRetDetail);
			                        }
								} else {
									tmpRespCode = "-1";
									tmpRespInfo = xmlReader.getFirstText("/MasMessage/PciQueryContent/responseTextMessage");
								}
							}
						} else {
							tmpRespCode = "-1";
							tmpRespInfo = smsResp.retMessage;
						}
					} else {
						tmpRespCode = "-1";
						tmpRespInfo = smsResp.retMessage;
					}
				}
			} else {
				tmpRespCode = "-1";
				tmpRespInfo = "未找到账号:[" + tmpQryReq.recAct + "]商户配置信息!";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRespCode = "-1";
			tmpRespInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		if (retAts.getWrittenRdCount() > 0) {
			tmpRespCode = "0";
		} else if ("-1".equals(tmpRespCode)) {
			retAts.writeRespCode(tmpRespCode);
			retAts.writeRespInfo(tmpRespInfo);
		} else if (retAts.getWrittenRdCount() == 0) {
			retAts.writeRespCode("0");
			retAts.writeRespInfo("客户信息为空");
		} 
		
		retAts.writeEnd();
		return retAts;
	}
}
